var Material = function() {};

Material.prototype.sample = function(ray, position, normal, scene) {
	var u = position.dot(scene.right) / 0.6;
	var v = position.dot(scene.up) / 0.6;
	u -= Math.floor(u);
	v -= Math.floor(v);
	u = Math.floor(u * 420);
	v = Math.floor(v * 420);
	var i = (420 - v) * 420 * 4 + u * 4;
	return new Color(imgPixels[i] / 255, imgPixels[i + 1] / 255, imgPixels[i + 2] / 255);
//	return Math.abs((Math.floor(u * this.scale) + Math.floor(v * this.scale)) % 2) < 1 ? this.black : Color.white;
};

var CheckerMaterial = function(diffuse, specular, reflect, refract, scale) {
	this.specular = specular;
	this.diffuse = diffuse;
	this.reflect = reflect;
	this.refract = refract;
	this.scale = scale;
	this.black = new Color(0.1, 0.1, 0.1);
};

CheckerMaterial.prototype = new Material();

var PhoneMaterial = function(diffuse, specular, reflect, refract, color, rindex) {
	this.diffuse = diffuse;
	this.specular = specular;
	this.reflect = reflect;
	this.refract = refract;
	this.color = color;
	this.rindex= rindex;
};

PhoneMaterial.prototype = new Material();

PhoneMaterial.prototype.sample = function(ray, position, normal, scene) {
	var vp = position.subtract(scene.center);
	var phi = Math.acos(-vp.dot(scene.vn));
	var u = phi / Math.PI;
	var theta = Math.acos(vp.dot(scene.ve) / Math.sin(phi)) * 0.5;
	var v;
	if(vp.dot(scene.vc) > 0){
		v = 1 - theta / Math.PI;
	}else{
		v = theta / Math.PI;
	}
	u -= Math.floor(u);
	v -= Math.floor(v);
	u = Math.floor(u * 1024);
	v = Math.floor(v * 1024);
	var i = (1024 - v) * 1024 * 4 + u * 4;
	var c = new Color(imgPixels2[i] / 255, imgPixels2[i + 1] / 255, imgPixels2[i + 2] / 255);
	return c;
//	return this.color;
};